using UnityEngine;
using UnityEngine.UI;
using QFramework;

namespace TowerDefence_QF
{
	public class GamePanelData : UIPanelData
	{
	}
	public partial class GamePanel : UIPanel,IController
	{
		private GameModel mGameModel;
		private LevelSystem mLevelSystem;
		protected override void OnInit(IUIData uiData = null)
		{
			mData = uiData as GamePanelData ?? new GamePanelData();
			mGameModel = this.GetModel<GameModel>();
			mLevelSystem = this.GetSystem<LevelSystem>();
			// please add init code here
			CountDownText.Hide();
			MenuPanel.Hide();
			HPText.text=mGameModel.HP.ToString();
			MenuBtn.onClick.AddListener(OnMenuBtnClick);
			ContinueBtn.onClick.AddListener(OnContinueBtnClick);
			ExitBtn.onClick.AddListener(OnExitBtnClick);

			StringEventSystem.Global.Register(NameData.END_LEVEL, EndLevel);
			
			this.RegisterEvent<UdpateHPEvent>(e =>
			{
				UdpateHPText(e.HP);
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			
		}
		
		protected override void OnOpen(IUIData uiData = null)
		{
		}
		
		protected override void OnShow()
		{
		}
		
		protected override void OnHide()
		{
		}
		
		protected override void OnClose()
		{
			MenuBtn.onClick.RemoveListener(OnMenuBtnClick);
			ContinueBtn.onClick.RemoveListener(OnContinueBtnClick);
			ExitBtn.onClick.RemoveListener(OnExitBtnClick);
			StringEventSystem.Global.UnRegister(NameData.END_LEVEL, EndLevel);
		}

		private void OnMenuBtnClick()
		{
			mLevelSystem.SetIsCreatEnemy(false);
			MenuPanel.Show();
		}

		private void OnContinueBtnClick()
		{
			mLevelSystem.SetIsCreatEnemy(true);
			MenuPanel.Hide();
		}

		private void OnExitBtnClick()
		{
			StringEventSystem.Global.Send(NameData.END_LEVEL);
			
		}

		private void EndLevel()
		{
			MenuPanel.Hide();
			this.CloseSelf();
			UIKit.OpenPanel<MainInterface>();
		}

		private void UdpateHPText(int hp)
		{
			HPText.text=hp.ToString();
		}

		public IArchitecture GetArchitecture()
		{
			return GameApp.Interface;
		}
	}
}
